@extends('layout')
@section('content')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i>
        当前位置: <span class="c-gray en">&gt;</span>
        渠道管理 <span class="c-gray en">&gt;</span>
    <a style="color:blue;" href="{{ url('nonPublicVersion/channel') }}" >渠道列表</a> <span class="c-gray en">&gt;</span>
        编辑渠道
    </nav>
    <article class="page-container">
        <form action="{{ url('nonPublicVersion/channel/' . $oldData->id) }}" method="PUT" class="form form-horizontal" id="form-member-add">
            {{ csrf_field() }}
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>渠道名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="{{ $oldData->name }}" placeholder="" id="name" name="name">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>所属父渠道：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <span class="select-box">
                        <select class="select" size="1" name="pid" id="pid">
                            <option value="" >--请选择--</option>
                            @foreach($topChannel as $channel)
                                <option value="{{ $channel->id }}"
                                        @if($oldData->pid == $channel->id)
                                        selected
                                        @endif>{{ $channel->name }}
                                </option>
                            @endforeach
                        </select>
				    </span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>一级渠道：</label>
                <div class="formControls col-xs-7 col-sm-8"> <span class="select-box">
				<select class="select" size="1" name="one_channel" id="one_channel">
					<option value="" >--请选择--</option>
                    @foreach($oneChannel as $channel)
                        <option value="{{ $channel->id }}"
                                @if($oldData->one_channel == $channel->id)
                                selected
                                @endif>{{ $channel->name }}
                        </option>
                    @endforeach
				</select>
				</span>
                </div>
                <button class="btn btn-info" type="button" onclick="showDialog()">
                    新增
                </button>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">二级渠道：</label>
                <div class="formControls col-xs-7 col-sm-8"> <span class="select-box">
				<select class="select" size="1" name="two_channel" id="two_channel">
					<option value="" selected>--请选择--</option>
                    @foreach($currentTwoChannel as $channel)
                        <option value="{{ $channel->id }}"
                                @if($oldData->two_channel == $channel->id)
                                selected
                                @endif>{{ $channel->name }}
                        </option>
                    @endforeach
				</select>
				</span>
                </div>
                <button class="btn btn-info" type="button" onclick="addTwoChannel()">
                    新增
                </button>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">合作方式：</label>
                <div class="formControls col-xs-7 col-sm-8"> <span class="select-box">
				<select class="select" size="1" name="cooperate_type_id" id="cooperate_type_id">
					<option value="" selected>--请选择--</option>
                    @foreach($cooperates as $cooperate)
					    <option value="{{ $cooperate->id }}"
                                @if($oldData->cooperate_type_id == $cooperate->id)
                                    selected
                                @endif>{{ $cooperate->name }}
                        </option>
                    @endforeach
				</select>
				</span>
                </div>
                <button class="btn btn-info" type="button" onclick="addCooperateType()">
                    新增
                </button>
            </div>

            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                </div>
            </div>
        </form>

        {{--增加一级渠道--}}
        <div class="page-container add-one-channel" style="display: none;">
            <form action="{{ url('nonPublicVersion/channel') }}" method="post" id="one">
                {{ csrf_field() }}
                <input type="hidden" name="is_one_channel" value="1">
                <input type="hidden" name="pid" value="" id="one-father">
                <div class="layer_notice">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">
                            <span class="c-red">*</span>一级渠道：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="one_name" name="name">
                        </div>
                    </div>
                </div>
                <div class="row cl">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;确定&nbsp;&nbsp;">
                        <input class="btn btn-primary radius" type="button" onclick="closeDialog()" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
                    </div>
                </div>
            </form>
        </div>

        {{--增加二级渠道--}}
        <div class="page-container add-two-channel" style="display: none;">
            <form action="{{ url('nonPublicVersion/channel') }}" method="post" id="two">
                {{ csrf_field() }}
                <input type="hidden" name="is_two_channel" value="1">
                <input type="hidden" name="pid" value="" id="father">
                <input type="hidden" name="two_father" value="" id="two-father">
                <div class="layer_notice">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">
                            <span class="c-red">*</span>二级渠道：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="tow_name" name="name">
                        </div>
                    </div>
                </div>
                <div class="row cl">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;确定&nbsp;&nbsp;">
                        <input class="btn btn-primary radius" type="button" onclick="closeAddTwoChannel()" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
                    </div>
                </div>
            </form>
        </div>

        {{--增加合作方式--}}
        <div class="page-container add-cooperate" style="display: none;">
            <form action="{{ url('nonPublicVersion/cooperate') }}" method="post" id="add-cooperate">
                {{ csrf_field() }}
                <div class="layer_notice">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">
                            <span class="c-red">*</span>合作方式：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="cooperate-name" name="name">
                        </div>
                    </div>
                </div>
                <div class="row cl">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;确定&nbsp;&nbsp;">
                        <input class="btn btn-primary radius" type="button" onclick="closeCooperateIndex()" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
                    </div>
                </div>
            </form>
        </div>
    </article>
@endsection
@section('script')
    <script type="application/javascript">
        var index = '';
        var indexs = '';
        var cooperateIndex;
        function addCooperateType() {
            cooperateIndex = layer.open({
                type: 1,
                title:'新增',
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '240px'], //宽高
                content: $('.add-cooperate')
            });
        }

        function showDialog() {
            var pid = $('#pid').val();
            if (!pid){
                //layer.msg('请选择所属父渠道');
                //return;
            }
            $('#one-father').val(pid);
            index = layer.open({
                type: 1,
                title:'新增',
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '240px'], //宽高
                content: $('.add-one-channel')
            });
        }

        function addTwoChannel() {
            var pid = $('#pid').val();
            var one_channel = $('#one_channel').val();

            if (!one_channel){
                layer.msg('请选择一级渠道');
                return;
            }

            $('#father').val(one_channel);
            indexs = layer.open({
                type: 1,
                title:'新增',
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '240px'], //宽高
                content: $('.add-two-channel')
            });
        }

        function closeAddTwoChannel() {
            layer.close(indexs);
        }

        function closeDialog() {
            layer.close(index);
        }

        function closeCooperateIndex() {
            layer.close(cooperateIndex);
        }

        $(function () {
            /*$('#pid').change(function () {
                $.ajax({
                    url:'getChild',
                    method:'post',
                    data:{pid:$(this).val()},
                    success:function (data) {
                        $('#one_channel').html(data);
                    }
                })
            });*/

            $('#one_channel').change(function () {
                $.ajax({
                    url:'/nonPublicVersion/channel/getChild',
                    method:'post',
                    data:{pid:$(this).val()},
                    success:function (data) {
                        $('#two_channel').html(data);
                    }
                })
            });

            $("#form-member-add").submit(function () {
                var url = $(this).attr('action');
                request(url,function (data) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.$('#btn-refresh').click();
                    parent.layer.close(index);
                },$(this).serialize(),'PUT');
                return false;
            });


            $("#one").submit(function () {
                var url = $(this).attr('action');
                var params = {
                    is_one_channel:1,
                    pid:'1',
                    name:$("#one_name").val(),
                    one_channel:'1',
                };
                request(url,function (data) {
                    $('#one_channel').html(data);
                    closeDialog();
                },params,'post');
                return false;
            });


            $("#two").submit(function () {
                var url = $(this).attr('action');
                var pid = $("#pid").val();
                var one_channel = $('#one_channel').val();

                var params = {
                    is_two_channel:1,
                    pid:one_channel,
                    one_channel:1,
                    name:$("#tow_name").val(),
                };
                request(url,function (data) {
                    $('#two_channel').html(data);
                    closeAddTwoChannel();
                },params,'post');
                return false;
            });

            $("#add-cooperate").submit(function () {
                var url = $(this).attr('action');
                var data = $("#cooperate-name").val();
                if( data.length  >= 20 ){
                    layer.confirm('最多输入20个字符', {
                        btn: ['确定','取消'] //按钮
                    });
                    return false;
                }
                request(url,function (data) {
                    $('#cooperate_type_id').html(data);
                    closeCooperateIndex();
                },$(this).serialize(),'post');
                return false;
            });
        })
    </script>
@endsection